package com.coocaa.ops.admin.v1.redis;

import com.coocaa.ops.admin.v1.redis.service.CacheMgrService;
import com.coocaa.ops.common.core.base.model.R;
import com.coocaa.ops.common.core.biz.cache.constant.MatchConstants;
import com.coocaa.ops.common.core.biz.cache.model.query.DelKeyModel;
import com.coocaa.ops.common.core.biz.cache.validator.DeleteCacheValidator;
import com.coocaa.ops.common.core.security.util.SecurityUtil;
import com.coocaa.ops.common.tool.regex.RegexUtil;
import com.coocaa.ops.plus.core.base.annotation.MyAuth;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author bijiahao
 * @date : 2019/6/24.
 * @description 我的缓存 可视化管理 控制器
 */
@RestController
@RequestMapping("/myCache")
public class RedisController {
    @Autowired
    private CacheMgrService cacheMgrService;

    @RequestMapping(value = "/updateCache", produces = "text/html;charset=UTF-8")
    public R updateCache(HttpServletRequest request, @RequestParam Integer id, @RequestParam String type) {
//        LimiterMgr.UPDATE_CACHE_LIMITER.acquire(1);
//        if (!UpdateCacheValidator.isTypeUpdateCache(type)) {
//            return R.fail("没有对应的资源类型的处理！");
//        }
//        multiCenterCacheUpdateService.updateMultiCenterCache(id, type);
        return R.ok();
    }

    @RequestMapping(value = "/listKeyType", produces = "text/html;charset=UTF-8")
    public R listKeyType(HttpServletRequest request) {
        return cacheMgrService.listKeyType();
    }

    @MyAuth
    @RequestMapping(value = "/listKey", produces = "text/html;charset=UTF-8")
    public R listKey(HttpServletRequest request,
                     @RequestParam String key,
                     @RequestParam String cacheEnv,
                     @RequestParam(required = false) Integer pageSize,
                     @RequestParam(required = false) Integer scanCount,
                     @RequestParam(required = false) Integer match) {
        if (StringUtils.isBlank(key)) {
            return R.fail("key 不能为空");
        }
        if (RegexUtil.matchChinese(key)) {
            return R.fail("key 不能为中文");
        }
        if (null == pageSize) {
            pageSize = 15;
        }
        if (null == match) {
            match = MatchConstants.PATTERN;
        }
        return cacheMgrService.listKey(key, cacheEnv, scanCount, pageSize , match);
    }

    @MyAuth
    @RequestMapping(value = "/getValByKey", produces = "text/html;charset=UTF-8")
    public R getValByKey(HttpServletRequest request, @RequestParam String key, @RequestParam String cacheEnv) {
        if (StringUtils.isBlank(key)) {
            return R.fail("key 不能为空");
        }
        return cacheMgrService.getValByKey(key, cacheEnv);
    }

    @MyAuth
    @RequestMapping(value = "/delByKey", produces = "text/html;charset=UTF-8")
    public R delByKey(HttpServletRequest request, DelKeyModel model) {
        if (ArrayUtils.isEmpty(model.getKeys())) {
            return R.fail("keys 不能为空");
        }
        if (DeleteCacheValidator.isDisablePatternDel(model.getMatch(), request)) {
            return R.fail("当前模糊删除操作 不允许");
        }
        if (null == model.getMatch()) {
            model.setMatch(MatchConstants.ABSOLUTE);
        }
        model.setSign(SecurityUtil.getSign(null, model));
        if (DeleteCacheValidator.isEnabelPatternDel(model.getMatch(), request)) {
            return cacheMgrService.delByKey(model.getKeys(), model.getMatch(), model.getCacheEnv());
        }
        return R.ok();
    }

    @RequestMapping(value = "/ttl", produces = "text/html;charset=UTF-8")
    public R ttl(HttpServletRequest request, @RequestParam String key, @RequestParam String cacheEnv) {
        if (StringUtils.isEmpty(key)) {
            return R.fail("key 不能为空");
        }
        return cacheMgrService.ttl(key, cacheEnv);
    }
}
